package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.common.vo.UserVo;
import com.xz.courseschedule.entity.User;
import com.xz.courseschedule.mapper.UserMapper;
import com.xz.courseschedule.service.UserService;
import com.xz.courseschedule.utils.JWTUtils;
import com.xz.courseschedule.utils.MD5Utils;
import com.xz.courseschedule.utils.Result;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zys
 * @CreateTime: 2025-05-22
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result login(UserVo userVo) {
        User user = super.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, userVo.getUsername()));
        if (user==null){
            return Result.error(400,"用户不存在");
        }
        if (!MD5Utils.encrypt(userVo.getPassword()).equals( user.getPassword())){
            return Result.error(400,"账户或密码错误");
        }
        user.setPassword("");
        Map<String, String> payload = new HashMap<>();
        payload.put("id", String.valueOf(user.getId()));
        payload.put("username", user.getUsername());
        String token = JWTUtils.getToken(payload);
        Map<String, String> result = new HashMap<>();
        result.put("token",token);
        return Result.success(result);
    }
}
